/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#ifndef _HDFS_LIBHDFS3_CLIENT_KERBEROSNAME_H_
#define _HDFS_LIBHDFS3_CLIENT_KERBEROSNAME_H_

#include <string>
#include <sstream>

#include <kmhdfs/common/hash.h>

namespace Hdfs {
namespace Internal {

class KerberosName {
public:
    KerberosName();
    KerberosName(const std::string & principal);

    std::string getPrincipal() const {
        std::stringstream ss;
        ss.imbue(std::locale::classic());
        ss << name;

        if (!host.empty()) {
            ss << "/" << host;
        }

        if (!realm.empty()) {
            ss << '@' << realm;
        }

        return ss.str();
    }

    const std::string & getHost() const {
        return host;
    }

    void setHost(const std::string & host) {
        this->host = host;
    }

    const std::string & getName() const {
        return name;
    }

    void setName(const std::string & name) {
        this->name = name;
    }

    const std::string & getRealm() const {
        return realm;
    }

    void setRealm(const std::string & realm) {
        this->realm = realm;
    }

    size_t hash_value() const;

    bool operator ==(const KerberosName & other) const {
        return name == other.name && host == other.host && realm == other.realm;
    }

private:
    void parse(const std::string & principal);

private:
    std::string name;
    std::string host;
    std::string realm;
};

}
}

HDFS_HASH_DEFINE(::Hdfs::Internal::KerberosName);

#endif /* _HDFS_LIBHDFS3_CLIENT_KERBEROSNAME_H_ */
